using System;
using System.Collections;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace BMC.ARSystem
{
	[ClassInterface, Guid("3E71FD58-EC2E-4f5c-A8B1-13A37275B565"), ProgId("BMC.ARSystem.Entry")]
	public abstract class Entry
	{
		private Server server;
		protected string entryId;
		private FieldValueList fieldValueList;
		protected abstract ARForm FormDefinition
		{
			get;
			set;
		}
		protected abstract ArrayList Fields
		{
			get;
			set;
		}
		public abstract string FormName
		{
			get;
		}
		protected FieldValueList Data
		{
			get
			{
				return this.fieldValueList;
			}
		}
		[DebuggerHidden]
		public string EntryId
		{
			get
			{
				return this.entryId;
			}
		}
		public Entry(Server server, string formName)
		{
			this.server = server;
			if (this.FormDefinition == null || this.Fields == null)
			{
				this.FormDefinition = server.GetForm(formName);
				this.Fields = server.GetListField(formName, 1);
			}
			this.fieldValueList = new FieldValueList();
		}
		public Entry(Server server, string formName, string entryId) : this(server, formName)
		{
			this.entryId = entryId;
			this.fieldValueList = server.GetEntry(formName, entryId);
		}
		public Entry(Server server, string formName, string entryId, FieldValueList fieldValueList) : this(server, formName, entryId)
		{
			this.fieldValueList = fieldValueList;
		}
		public string CreateEntry()
		{
			this.entryId = this.server.CreateEntry(this.FormName, this.Data);
			return this.entryId;
		}
		public void SetEntry()
		{
			this.server.SetEntry(this.FormName, this.entryId, this.Data);
		}
		public void DeleteEntry()
		{
			Entry.DeleteEntry(this.server, this.FormName, this.entryId);
		}
		public static void DeleteEntry(Server server, string formName, string entryId)
		{
			server.DeleteEntry(formName, entryId, 1);
		}
		public static ArrayList Query(Type typ, Server server, string formName, string qualification, int[] fieldIdList, uint firstRetrieve, uint maxRetrieve, out int numMatches, ArrayList sortInfoList, string vuiName, bool useLocale)
		{
			EntryFieldValueList listEntryWithFields = server.GetListEntryWithFields(formName, qualification, fieldIdList, firstRetrieve, maxRetrieve, out numMatches, sortInfoList, vuiName, useLocale);
			ArrayList arrayList = new ArrayList(listEntryWithFields.get_Count());
			foreach (EntryFieldValue current in listEntryWithFields)
			{
				object obj = typ.InvokeMember(typ.get_Name(), 532, null, null, new object[]
				{
					server,
					current.EntryId,
					current.FieldValues
				});
				arrayList.Add(obj);
			}
			return arrayList;
		}
		public static ArrayList QueryAndRetrieveIteratively(Type typ, Server server, string formName, string qualification, int[] fieldIdList, uint firstRetrieve, uint maxRetrieve, out int numMatches, ArrayList sortInfoList, string vuiName, bool useLocale)
		{
			ArrayList listEntry = server.GetListEntry(formName, qualification, null, firstRetrieve, maxRetrieve, out numMatches, sortInfoList, vuiName, useLocale);
			ArrayList arrayList = new ArrayList(listEntry.get_Count());
			IEnumerator enumerator = listEntry.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					EntryDescription entryDescription = (EntryDescription)enumerator.get_Current();
					object obj = typ.InvokeMember(typ.get_Name(), 532, null, null, new object[]
					{
						server,
						entryDescription.EntryId
					});
					arrayList.Add(obj);
				}
			}
			finally
			{
				IDisposable disposable = enumerator as IDisposable;
				if (disposable != null)
				{
					disposable.Dispose();
				}
			}
			return arrayList;
		}
		protected void setFieldValue(uint fieldId, int typ, object val)
		{
			if (this.Data.Contains(fieldId))
			{
				this.Data.Remove(fieldId);
			}
			this.Data.Add(fieldId, val, typ);
		}
		public void setFieldToKeywordValue(uint fieldId, Keyword val)
		{
			if (this.Data.Contains(fieldId))
			{
				this.Data.Remove(fieldId);
			}
			this.Data.Add(fieldId, val, 1);
		}
		private static Array ResizeArray(Array oldArray, int newSize)
		{
			int length = oldArray.get_Length();
			Type elementType = oldArray.GetType().GetElementType();
			Array array = Array.CreateInstance(elementType, newSize);
			int num = Math.Min(length, newSize);
			if (num > 0)
			{
				Array.Copy(oldArray, array, num);
			}
			return array;
		}
		protected static int[] GetQueryListFields(ArrayList allDataFields)
		{
			uint[] array = (uint[])allDataFields.ToArray(typeof(uint));
			int[] array2 = new int[array.Length];
			int num = 0;
			int i = 0;
			int num2 = array.Length;
			while (i < num2)
			{
				if (array[i] == 15u)
				{
					num++;
				}
				else
				{
					array2[i - num] = Convert.ToInt32(array[i]);
				}
				i++;
			}
			return (int[])Entry.ResizeArray(array2, array.Length - num);
		}
	}
}
